package remote.service;

import client.common.dto.ItemReportDTO;
import client.common.dto.MaterialAddDTO;
import client.common.dto.MaterialUpdateDTO;
import client.common.result.MaterialPageResult;
import client.common.result.Result;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import remote.common.constant.UrlConstant;
import remote.common.storage.TokenStorage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料管理接口
 */
public interface MaterialService {
    /**
     * 获取物料分页列表
     *
     * @param current    当前页
     * @param size       每页数量
     * @param filterNull 是否过滤空值
     * @return 物料分页列表
     */
    default MaterialPageResult getMaterialByPage(int current, int size, boolean filterNull) {
        HashMap<String, Object> requestParam = new HashMap<>();
        requestParam.put("current", current);
        requestParam.put("size", size);
        requestParam.put("filterNull", filterNull);
        //todo 修改token由登陆后获取
        // 向服务器发送登录请求
        String resultBodyStr = HttpRequest.get(UrlConstant.BASE_URL + "/materials/getMaterialByPage").header("Authorization", TokenStorage.getToken())
                .form(requestParam).execute().body();
//        String resultBodyStr = HttpUtil.get(UrlConstant.BASE_URL+"/materials/getMaterialByPage",requestParam);
        System.out.println("resultBodyStr: " + resultBodyStr);
        MaterialPageResult result = JSON.parseObject(resultBodyStr, MaterialPageResult.class);
        return result;
    }
    
    ;
    
    /**
     * 更新物料
     *
     * @param materialUpdateDTO 物料更新DTO
     * @return 更新结果
     */
    default Result updateMaterial(MaterialUpdateDTO materialUpdateDTO) {
        String resultStr = HttpRequest.put(UrlConstant.BASE_URL + "/materials/updateMaterial")
                .header("Authorization", TokenStorage.getToken())
                .body(JSON.toJSONString(materialUpdateDTO))
                .execute().body();
        Result result = JSON.parseObject(resultStr, Result.class);
        return result;
    }
    
    ;
    
    /**
     * 模糊查询物料接口
     */
    default MaterialPageResult fuzzyQueryMaterial(String keyword, Integer current,
                                                  Integer size, boolean filterNull) {
        Map<String, Object> requestParam = new HashMap<>();
        if (!(keyword == null || keyword.isEmpty())) {
            requestParam.put("searchText", keyword);
        }
        requestParam.put("current", current);
        requestParam.put("size", size);
        requestParam.put("filterNull", filterNull);
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/materials/getMaterialByPage")
                .header("Authorization", TokenStorage.getToken())
                .form(requestParam)
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, MaterialPageResult.class);
        
    }
    
    /**
     * 删除物料接口
     */
    default Result deleteMaterialById(Long id) {
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("id", id);
        String resultStr = HttpRequest.delete(UrlConstant.BASE_URL + "/materials/deleteMaterialById")
                .header("Authorization", TokenStorage.getToken())
                .form(requestParam)
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, Result.class);
        
    }
    
    /**
     * 新增物料品类接口
     *
     * @return 新增结果
     */
    default Result addMaterial(MaterialAddDTO materialAddDTO) {
        String materialJSON = JSON.toJSONString(materialAddDTO);
        String resultStr = HttpRequest.post(UrlConstant.BASE_URL + "/materials/addMaterialType" + "?materialJSON=" + materialJSON)
                .header("Authorization", TokenStorage.getToken())
                
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, Result.class);
    }
    
    /**
     * 获取仓库物料列表接口
     *
     * @param materialId
     * @param year
     * @return
     */
    default Result<List<ItemReportDTO>> getReportByMaterialIdAndYear(Long materialId, String year) {
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("materialId", materialId);
        requestParam.put("year", year);
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/materials/getWarehouseLedger")
                .header("Authorization", TokenStorage.getToken())
                .form(requestParam)
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<ItemReportDTO>>>() {
        });
    }
}
